<?php

class Admin_PiloteController extends Zend_Controller_Action
{

    public function init()
    {
		if(!Zend_Auth::getInstance()->hasIdentity())  
		{
			$this->_redirect('admin/login');  
		}  
    }

    public function indexAction()
    {
    }
    
    public function addAction()
    {
    	$form = new Application_Form_Pilote();
    	$piloteDAO = new Application_Model_PiloteDAO();
    	$paysDAO = new Application_Model_PaysDAO();
    	$ecurieDAO = new Application_Model_EcurieDAO();
		$this->view->form = $form;
		if($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData))
			{
				$values = $form->getValues();
				$pilote = new Application_Model_Pilote();
				$pilote->setPays($paysDAO->find($values['paynum']));
				$pilote->setPilNom($values['pilnom']);
				$pilote->setPilPrenom($values['pilprenom']);
				$pilote->setPilDateNais($values['pildatenais']);
				$pilote->setPilPigiste(0);
				$pilote->setPilPoints(0);
				$pilote->setPilTaille($values['piltaille']);
				$pilote->setPilPoids($values['pilpoids']);
				$pilote->setPilTexte($values['piltexte']);
				$pilote->setEcurie($ecurieDAO->find($values['ecunum']));

				if($piloteDAO->save($pilote))
				{
					$this->_redirect('admin/pilote');
				}
				else
				{
					$form->populate($values);
				}
			}
			else
			{
				$form->populate($form->getValues());
			}
		}
    }
    
    public function updateAction()
    {
    	$piloteDAO = new Application_Model_PiloteDAO();
    	$id = $this->getRequest()->getParam('id');
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	        $form = new Application_Form_Pilote();
	    	$paysDAO = new Application_Model_PaysDAO();
	    	$ecurieDAO = new Application_Model_EcurieDAO();
	    	$pilote = $piloteDAO->find($id);
	    	if(!is_null($pilote))
	    	{
				$this->view->form = $form;
				$form->populate($pilote->toArray());
				if($this->getRequest()->isPost())
				{
					$formData = $this->getRequest()->getPost();
					if($form->isValid($formData))
					{
						$values = $form->getValues();
						$pilote = new Application_Model_Pilote();
						$pilote->setPilNum($values['pilnum']);
						$pilote->setPays($paysDAO->find($values['paynum']));
						$pilote->setPilNom($values['pilnom']);
						$pilote->setPilPrenom($values['pilprenom']);
						$pilote->setPilDateNais($values['pildatenais']);
						$pilote->setPilPigiste(0);
						$pilote->setPilPoints(0);
						$pilote->setPilTaille($values['piltaille']);
						$pilote->setPilPoids($values['pilpoids']);
						$pilote->setPilTexte($values['piltexte']);
						$pilote->setEcurie($ecurieDAO->find($values['ecunum']));
		
						if($piloteDAO->save($pilote))
						{
							$this->_redirect('admin/pilote');
						}
						else
						{
							$form->populate($values);
						}
					}
					else
					{
						$form->populate($form->getValues());
					}
				}
			}
			else
			{
				$this->_redirect('admin/pilote');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->pilotes = $piloteDAO->findAll();
    	}
    }
	
    public function deleteAction()
    {
        $piloteDAO = new Application_Model_PiloteDAO();
    	$id = $this->getRequest()->getParam('id');
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	    	$pilote = $piloteDAO->find($id);
	    	if(!is_null($pilote))
	    	{
				$piloteDAO->delete($pilote->getPilNum());
				$this->_redirect('admin/pilote');
			}
			else
			{
				$this->_redirect('admin/pilote');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->pilotes = $piloteDAO->findAll();
    	}
    }
}